{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "665f42aa-9e3f-42d1-888b-126ef79fc393",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Program Files\\anaconda3\\lib\\site-packages\\pandas\\util\\testing.py:27: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar.\n",
      "  import pandas._libs.testing as _testing\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "module 'numpy' has no attribute 'bool'.\n`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\nThe aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:\n    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-180d1b34fc7e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodel_selection\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtrain_test_split\u001b[0m \u001b[1;31m# 用于将数据集随机划分为 训练集 和 测试集\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program Files\\anaconda3\\lib\\site-packages\\pandas\\__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m    180\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    181\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mutil\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_tester\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtest\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 182\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtesting\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    183\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marrays\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    184\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program Files\\anaconda3\\lib\\site-packages\\pandas\\testing.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \"\"\"\n\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m from pandas.util.testing import (\n\u001b[0m\u001b[0;32m      8\u001b[0m     \u001b[0massert_frame_equal\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[0massert_index_equal\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program Files\\anaconda3\\lib\\site-packages\\pandas\\util\\testing.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     25\u001b[0m )\n\u001b[0;32m     26\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 27\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_libs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtesting\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0m_testing\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     28\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcompat\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0m_get_lzma_file\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_import_lzma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mraise_with_traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     29\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mpandas/_libs/testing.pyx\u001b[0m in \u001b[0;36minit pandas._libs.testing\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mD:\\Program Files\\anaconda3\\lib\\site-packages\\numpy\\__init__.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(attr)\u001b[0m\n\u001b[0;32m    303\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    304\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mattr\u001b[0m \u001b[1;32min\u001b[0m \u001b[0m__former_attrs__\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 305\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m__former_attrs__\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mattr\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    306\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    307\u001b[0m         \u001b[1;31m# Importing Tester requires importing all of UnitTest which is not a\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'numpy' has no attribute 'bool'.\n`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\nThe aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:\n    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split # 用于将数据集随机划分为 训练集 和 测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "993993db-0496-41f6-90eb-e285f794f884",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'pd' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-7b795a882eeb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 1. 竞标行为数据集(shill_bidding.csv)是网络交易平台eBay为了分析竞标者的竞标行为而收集整理的部分拍卖数据\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# 读取数据集\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'./data/shill_bidding.csv'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'GBK'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'pd' is not defined"
     ]
    }
   ],
   "source": [
    "# 1. 竞标行为数据集(shill_bidding.csv)是网络交易平台eBay为了分析竞标者的竞标行为而收集整理的部分拍卖数据\n",
    "# 读取数据集\n",
    "data = pd.read_csv('./data/shill_bidding.csv',encoding='GBK')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "b457290e-2f39-42fa-bafd-ff97b2921446",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>记录ID</th>\n",
       "      <th>拍卖ID</th>\n",
       "      <th>竞标者倾向</th>\n",
       "      <th>竞标比率</th>\n",
       "      <th>连续竞标</th>\n",
       "      <th>上次竞标</th>\n",
       "      <th>竞标量</th>\n",
       "      <th>拍卖起拍</th>\n",
       "      <th>早期竞标</th>\n",
       "      <th>胜率</th>\n",
       "      <th>拍卖持续时间（小时）</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>732</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000028</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.993593</td>\n",
       "      <td>0.000028</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>732</td>\n",
       "      <td>0.024390</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.013123</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.993593</td>\n",
       "      <td>0.013123</td>\n",
       "      <td>0.944444</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>732</td>\n",
       "      <td>0.142857</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003042</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.993593</td>\n",
       "      <td>0.003042</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>732</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.097477</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.993593</td>\n",
       "      <td>0.097477</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>900</td>\n",
       "      <td>0.051282</td>\n",
       "      <td>0.222222</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001318</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.001242</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   记录ID  拍卖ID     竞标者倾向      竞标比率  连续竞标      上次竞标  竞标量      拍卖起拍      早期竞标  \\\n",
       "0     1   732  0.200000  0.400000   0.0  0.000028  0.0  0.993593  0.000028   \n",
       "1     2   732  0.024390  0.200000   0.0  0.013123  0.0  0.993593  0.013123   \n",
       "2     3   732  0.142857  0.200000   0.0  0.003042  0.0  0.993593  0.003042   \n",
       "3     4   732  0.100000  0.200000   0.0  0.097477  0.0  0.993593  0.097477   \n",
       "4     5   900  0.051282  0.222222   0.0  0.001318  0.0  0.000000  0.001242   \n",
       "\n",
       "         胜率  拍卖持续时间（小时）  类别  \n",
       "0  0.666667           5   0  \n",
       "1  0.944444           5   0  \n",
       "2  1.000000           5   0  \n",
       "3  1.000000           5   0  \n",
       "4  0.500000           7   0  "
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看前5行数据\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "24ad2930-52e5-419b-80ea-d990d62190d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (2)对竞标行为数据集的数据和标签进行划分。\n",
    "# 划分特征和标签\n",
    "x = data.drop('类别', axis=1)  # 特征：影响竞标行为的输入变量\n",
    "y = data['类别']  # 标签：要预测的目标变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "716a44f4-787c-436d-9a69-1c75524840fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征形状: (6321, 11)\n"
     ]
    }
   ],
   "source": [
    "print(\"特征形状:\", x.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9fbc9f72-d4fd-491f-aab3-02bac7607a80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "标签形状: (6321,)\n"
     ]
    }
   ],
   "source": [
    "print(\"标签形状:\", y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "f1c2128c-3600-4ed6-8a00-4409624a1f00",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (3)将竞标行为数据集划分为训练集和测试集，测试集数据量占总样本数据量的 20%。\n",
    "# x 特征数据\n",
    "# y 标签数据\n",
    "# test_size 测试集比例（20%）\n",
    "# random_state 随机种子（保证每次划分一致）\n",
    "# shuffle 是否打乱数据（默认 True）\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=66,shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "ae8368b6-8cab-49cf-bfbd-7e0b7f32a6fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# PCA 降维\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "46cc8ed0-63fa-4300-b9ae-0fe872264c3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.62535096 -1.32306624  1.85323195 ... -1.11797798  1.44366595\n",
      "   0.97268997]\n",
      " [-0.89992308 -0.93169584  0.5653511  ... -0.16528005 -0.84482782\n",
      "   0.97268997]\n",
      " [ 0.14909017 -1.17988195  0.99464471 ...  1.29765346  1.44366595\n",
      "  -0.6469117 ]\n",
      " ...\n",
      " [-1.38989098 -0.65487287  0.101714   ... -0.00406694 -0.84482782\n",
      "   0.97268997]\n",
      " [-1.28478311 -1.640799    0.5653511  ...  0.32262881 -0.84482782\n",
      "  -1.45671254]\n",
      " [-1.16315173  1.53925597 -0.57534336 ... -0.9971409  -0.84482782\n",
      "   0.97268997]]\n",
      "[[-0.9930973   1.63062118  4.42899364 ...  0.96413945 -0.84482782\n",
      "   0.16288913]\n",
      " [ 0.41048288 -0.45577852  0.13605749 ... -1.10801427  0.68083469\n",
      "  -0.6469117 ]\n",
      " [-1.44864123 -0.97260564 -0.60272688 ...  1.01848986 -0.84482782\n",
      "   0.97268997]\n",
      " ...\n",
      " [-1.51427627 -0.09713596 -0.41949896 ...  0.90886303 -0.84482782\n",
      "   0.97268997]\n",
      " [-0.6474806   0.65969531 -0.37909485 ... -0.95263432  1.08232483\n",
      "   0.97268997]\n",
      " [-1.6510542  -0.81169376  1.33807961 ... -1.11863528  1.44366595\n",
      "  -0.6469117 ]]\n"
     ]
    }
   ],
   "source": [
    "# 数据标准化\n",
    "# 标准化 把数据变成均值为0，标准差为1的数据\n",
    "\n",
    "# 可以让模型公平的考虑\n",
    "# (记录ID\t拍卖ID\t竞标者倾向\t竞标比率\t连续竞标\t上次竞标\t竞标量\t拍卖起拍\t早期竞标\t胜率\t拍卖持续时间（小时）)\n",
    "# 这些数据的影响\n",
    "\n",
    "# 作用 \n",
    "# 消除单位：所有数据变成\"无单位的纯数值\"\n",
    "# 统一尺度：所有特征均值为0，标准差为1\n",
    "# 公平比较：算法不再受原始单位影响\n",
    "\n",
    "# StandardScaler : Z-score标准化: x(标准) = (x - 平均值)/标准差\n",
    "# 标准差 = 平方差**0.5\n",
    "# 平方差 = (每个数据与平均值的差)**2\n",
    "\n",
    "scaler = StandardScaler()\n",
    "# 用训练集计算均值和标准差，并立即转换训练集\n",
    "x_train_scaled = scaler.fit_transform(x_train)\n",
    "# scaler.transform() 使用的是训练集 的均值和标准差 转换测试集\n",
    "x_test_scaled = scaler.transform(x_test)\n",
    "print(x_train_scaled)\n",
    "print(x_test_scaled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "e4ec662f-13bd-46da-b09e-3fce8e40861c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (4)对竞标行为数据集进行 PCA 降维，设定n_components=0.999，即降维后数据能保留的信息为原来的 99.9%\n",
    "# PCA降维，保留99.9%的方差\n",
    "# 舍弃方差小的成分\n",
    "pca = PCA(n_components=0.999)\n",
    "x_train_pca = pca.fit_transform(x_train_scaled)\n",
    "x_test_pca = pca.transform(x_test_scaled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "baaf48c9-a7dd-42c4-bd63-f96b87a80809",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "降维后训练集形状: (5056, 11)\n",
      "降维后测试集形状: (1265, 11)\n",
      "保留的主成分数量: 11\n",
      "保留的方差比例: 1.0\n"
     ]
    }
   ],
   "source": [
    "# 查看降维后的形状\n",
    "print(\"降维后训练集形状:\", x_train_pca.shape)\n",
    "print(\"降维后测试集形状:\", x_test_pca.shape)\n",
    "print(\"保留的主成分数量:\", pca.n_components_)\n",
    "print(\"保留的方差比例:\", sum(pca.explained_variance_ratio_))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d5cec37-446d-4e4e-98ed-9491124bc9a5",
   "metadata": {},
   "source": [
    "# 构建线性回归模型并进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "fb1da73e-76b6-48bd-bee7-3900fb9ab267",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "b0522152-9147-45f6-9e17-d14dcb9d8f38",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建线性回归模型\n",
    "lr_model = LinearRegression()\n",
    "# 使用降维后的训练数据训练模型\n",
    "lr_model.fit(x_train_pca, y_train)\n",
    "# 预测测试集\n",
    "y_pred = lr_model.predict(x_test_pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "3fcadb12-71f7-4a25-8cd6-c40820db76b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算模型评估指标\n",
    "from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\n",
    "# 计算评估指标\n",
    "mae = mean_absolute_error(y_test, y_pred)\n",
    "mse = mean_squared_error(y_test, y_pred)\n",
    "r2 = r2_score(y_test, y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "73347fc9-99f8-4a6b-a686-1268bb51784f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均绝对误差(MAE):0.0512\n",
      "均方误差(MSE):0.0208\n",
      "R2分数:0.7937\n"
     ]
    }
   ],
   "source": [
    "print(f\"平均绝对误差(MAE):{mae:.4f}\")\n",
    "print(f\"均方误差(MSE):{mse:.4f}\")\n",
    "print(f\"R2分数:{r2:.4f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d22b222-0344-42e9-b8dd-022ddf680ac5",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1af5a54f-5654-475e-8e66-d1145f9f3177",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
